package com.wyu.dao;

import com.alibaba.druid.util.JdbcUtils;
import com.wyu.pojo.CG;
import com.wyu.pojo.YG;
import com.wyu.util.GetCt;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

public class CGDaoImpl implements CGDao {
    @Override
    public List<CG> searchcg() {
        QueryRunner qr = new QueryRunner();
        Connection ct = null;
        //获得数据库连接，德鲁伊数据源
        try {
            ct = GetCt.getConnection();
            String sql = "select * from cgb  ";
            List<String> list1 = new ArrayList<>();
            BeanListHandler<CG> handler = new BeanListHandler(CG.class);
            List<CG> list = qr.query(ct,sql,handler,list1.toArray());
            System.out.println(list);
            return list;
        } catch (Exception exception){
            exception.printStackTrace();
        }finally {
            JdbcUtils.close(ct);
        }
        return null;
    }

    @Override
    public int insertwp(String wpmc, String wpbh, String cgsl, String djr) {
        QueryRunner qr = new QueryRunner();
        Connection ct = null;
        //获得数据库连接，德鲁伊数据源
        try {
            ct = GetCt.getConnection();
            String sql = "insert into cgb (wpmc,wpbh,cgsl,djr) values (?,?,?,?);";
            int i = qr.update(ct,sql,wpmc,wpbh,cgsl,djr);
            if (i>0){
                System.out.println("成功添加"+i+"条数据");
                return i;
            }
        } catch (Exception exception){
            exception.printStackTrace();
        }finally {
            JdbcUtils.close(ct);
        }
        return 0;
    }

    @Override
    public int deletebyWpbh(String wpbh) {
        QueryRunner qr = new QueryRunner();
        Connection ct = null;
        //获得数据库连接，德鲁伊数据源
        try {
            ct = GetCt.getConnection();
            String sql = "delete from cgb where wpbh = ?;";
            int i = qr.update(ct,sql,wpbh);
            if (i>0){
                System.out.println("删除成功"+i+"条数据");
            }
            return i;
        } catch (Exception exception){
            exception.printStackTrace();
        }finally {
            JdbcUtils.close(ct);
        }
        return 0;
    }
}
